package com.wlzg.collectiontool.view

import android.content.Context
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.jaredrummler.materialspinner.MaterialSpinner
import com.lxj.xpopup.core.PositionPopupView
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.PsGongcheng
import com.wlzg.collectiontool.model.bean.PsXiangmu
import com.wlzg.collectiontool.model.bean.PsXiaoqu
import com.wlzg.collectiontool.util.DateUtil
import com.wlzg.collectiontool.util.QuyuPickerUtil
import kotlinx.android.synthetic.main.new_xiaoqu.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class NewXqDia(var mContext: Context, var mFragment: ManageFragment, var mpsxq: PsXiaoqu?,var dia:XiaoqulistDia?=null) :
    PositionPopupView(mContext),
    View.OnClickListener, MaterialSpinner.OnItemSelectedListener<Any> {
    var proList: ArrayList<PsXiangmu?>? = ArrayList()
    var gcList: ArrayList<PsGongcheng?>? = ArrayList()
    var quyuPickerUtil: QuyuPickerUtil? = null
    private var ssxmid: Long = 0;
    private var ssgcid: Long = 0;
    private var qyid: String? = null
    private var qyname: String? = null
    override fun getImplLayoutId(): Int {
        return R.layout.new_xiaoqu
    }

    override fun onCreate() {
        super.onCreate()
        shanchu.setOnClickListener(this)
        quxiao.setOnClickListener(this)
        diss.setOnClickListener(this)
        baocun.setOnClickListener(this)
        quyu.setOnClickListener(this)
//        initSpinner()
        quyuPickerUtil = QuyuPickerUtil(context) { id, name ->
            quyu.text = name
            qyid = id
            qyname = name

        }
        if (mpsxq != null) {
            initSpinner()

        }

    }

    override fun onShow() {
        super.onShow()
        if (mpsxq == null) {
            initSpinner()
        }
    }

    private fun initSpinner() {
        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                proList = AppDatabase.getInstance().userDao().getXmList() as ArrayList<PsXiangmu?>
                if (proList == null) {
                    proList = ArrayList()
                }
                if (proList!!.size > 0) {
                    val id = proList?.get(0)?.xiangmuId
                    gcList = id?.let {
                        AppDatabase.getInstance().userDao()
                            .getGcListBypid(it)
                    } as ArrayList<PsGongcheng?>
                }
                if (gcList == null) {
                    gcList = ArrayList()
                }
            }

            ssxm.setItems(proList as ArrayList<PsXiangmu>)
            ssxm.setOnItemSelectedListener(this@NewXqDia)
            ssgc.setItems(gcList as ArrayList<PsGongcheng>)
            ssgc.setOnItemSelectedListener(this@NewXqDia)

            if (proList != null) {
                if (proList!!.size > 0) {
                    ssxmid = proList!!.get(0)?.xiangmuId!!
                }
            }
            if (gcList != null) {
                if (gcList!!.size > 0) {
                    ssgcid = gcList!!.get(0)?.gcId!!
                }
            }
            mpsxq?.let {
                initData(it)
            }
        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.diss -> {
                mFragment.ondelete(5, null)
                dismiss()
            }
            R.id.shanchu -> {
                mFragment.ondelete(4, mpsxq)
                dismiss()
            }
            R.id.quxiao -> {
                mFragment.ondelete(4, null)
                dismiss()
            }
            R.id.baocun -> {
                save()
                dismiss()
            }
            R.id.quyu -> {
                if (quyuPickerUtil?.pvQuyu != null) {
                    if (!quyuPickerUtil?.pvQuyu!!.isShowing) {
                        quyuPickerUtil?.pvQuyu?.show()
                    }
                }
            }
        }
    }

    private fun save() {
        val xiaoqu = if (mpsxq != null) {
            mpsxq as PsXiaoqu
        } else {
            PsXiaoqu()
        }
        xiaoqu.villageName = xqName.text.toString()
        xiaoqu.locationcode = xqbm.text.toString()
        xiaoqu.ssgcId = ssgcid
        xiaoqu.ssxmId = ssxmid
        xiaoqu.districtId = qyid
        xiaoqu.districtName = qyname
        xiaoqu.updateTime=DateUtil.getCurData()
        dia?.onUpdate()
        mFragment.lifecycleScope.launch(context = Dispatchers.IO) {
            if (mpsxq == null) {
                val ids = AppDatabase.getInstance().userDao().insertPsxq(xiaoqu)
            } else {
                AppDatabase.getInstance().userDao().updatePsxq(xiaoqu)
            }
        }
    }

    private fun initData(xiaoqu: PsXiaoqu) {
        xqName.setText(xiaoqu.villageName)
        xqbm.setText(xiaoqu.locationcode)
        quyu.text = xiaoqu.districtName
        mFragment.lifecycleScope.launch(context = Dispatchers.IO) {
            val xm = AppDatabase.getInstance().userDao().getXmListbyId(xiaoqu.ssxmId)
            val gc = AppDatabase.getInstance().userDao().getGcListByid(xiaoqu.ssgcId)
            val xmindex = proList?.indexOf(xm)
            val gcindex = gcList?.indexOf(gc)
            withContext(Dispatchers.Main) {
                if (xmindex != null) {
                    ssxm.selectedIndex = xmindex
                }
                if (gcindex != null) {
                    ssgc.selectedIndex = gcindex
                }

            }
        }
    }

    override fun onItemSelected(view: MaterialSpinner?, position: Int, id: Long, item: Any?) {
        when (view?.id) {
            R.id.ssxm -> {
                mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
                    item as PsXiangmu
                    ssxmid = item.xiangmuId!!
                    Log.d("hsjjsjs", "id" + ssxmid)
                    withContext(Dispatchers.IO) {
                        gcList = AppDatabase.getInstance().userDao()
                            .getGcListBypid(item.xiangmuId!!) as ArrayList<PsGongcheng?>
                    }
                    if (gcList == null) {
                        gcList = ArrayList()
                    }
                    ssgc.setItems(gcList as ArrayList<PsGongcheng>)

                }
            }
            R.id.ssgc -> {
                item as PsGongcheng
                ssgcid = item.gcId!!
            }
        }
    }
}